<template>
  <div>
    <el-card class="margin-30" shadow="never">
      <span class="to-bold" style="line-height:20px;margin-right:5px;">订单信息:</span>
      <el-button v-if="orderDetail.status>=0" style="padding:0 5px;line-height:20px;" :type="returnStatus[orderDetail.status].type" :plain="returnStatus[orderDetail.status].plain" size="mini">{{ returnStatus[orderDetail.status].desc }}</el-button>
      <el-row style="margin-top:20px;margin-bottom:15px;font-size:14px;" :gutter="20">
        <el-col :span="8" :offset="0">
          订单编号:{{ orderDetail.orderSn }}
          <el-button type="text" size="mini" style="margin-left:5px;" @click="goToListDetail">查看订单详情</el-button>
          <!-- <router-link :to="{name:'OrderDetail',params:{orderSn:orderDetail.orderId}}">条猪阿奴</router-link> -->

        </el-col>
        <el-col :span="4" :offset="0">
          退单人姓名:{{ orderDetail.returnName }}
        </el-col>
        <el-col :span="6" :offset="0">
          退单理由:{{ orderDetail.reason }}
        </el-col>
        <el-col :span="6" :offset="0">
          退单描述:{{ orderDetail.description }}
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          申请时间:{{ orderDetail.createTime }}
        </el-col>
      </el-row>
      <el-row v-if="orderDetail.proofPics&&orderDetail.proofPics.length!=0" style="margin-top:20px;" :gutter="20">
        <el-col :span="12" :offset="0">
          <span>退单图片:</span>
          <el-image
            v-for="item in orderDetail.proofPics"
            :key="item"
            style="width: 100px; height: 100px;vertical-align:top;margin:0 10px;"
            :src="item"
            :preview-src-list="srcList"
          />
        </el-col>
      </el-row>

      <el-divider class="margin-top-30" />
      <div class="to-bold" style="margin-bottom:20px;">退货商品</div>
      <el-table :data="table" border stripe>
        <el-table-column label="图片" align="center" width="130">
          <template slot-scope="scope">
            <img width="100" height="100" :src="scope.row.productPic" alt="">
          </template>
        </el-table-column>

        <el-table-column label="商品描述" align="center" width="250">
          <template slot-scope="scope">
            <div>{{ scope.row.productName }}</div>
            <div>品牌:{{ scope.row.productBrand }}</div>
          </template>
        </el-table-column>

        <el-table-column label="商品价格" align="center" width="230">
          <template slot-scope="scope">
            <div>{{ scope.row.productRealPrice }}</div>
          </template>
        </el-table-column>

        <el-table-column label="商品属性" align="center" width="250">
          <template slot-scope="scope">
            <div>{{ scope.row.productAttr }}</div>
          </template>
        </el-table-column>

        <el-table-column label="商品数量" align="center" width="250">
          <template slot-scope="scope">
            <div>{{ scope.row.productCount }}</div>
          </template>
        </el-table-column>

        <el-table-column label="总计" align="center" width="250">
          <template slot-scope="scope">
            <div>{{ scope.row.returnAmount }}</div>
          </template>
        </el-table-column>
      </el-table>

      <div class="to-bold margin-top-30">用户信息</div>
      <el-row class="margin-top-30" :gutter="20">
        <el-col :span="6" :offset="0">
          联系人:{{ orderDetail.returnPhone }}
        </el-col>
        <el-col :span="6" :offset="0">
          联系电话:{{ orderDetail.returnPhone }}
        </el-col>
      </el-row>
      <el-divider class="margin-top-30" />

      <div class="to-bold margin-top-30">费用信息</div>
      <el-row style="margin:20px 0;" :gutter="0">
        <el-col :span="6" :offset="0">
          退单金额:<span style="color:red;">{{ orderDetail.returnAmount }}</span>
        </el-col>
        <el-col :span="6" :offset="0">
          运费金额:<span style="color:red;">0</span>
        </el-col>
      </el-row>

      <div v-if="orderDetail.status!==0">
        <el-divider class="margin-top-30" />
      </div>

      <el-form ref="form" style="transform:translateX(-10px)" :model="form" :rules="rules" label-width="80px" :inline="false" size="mini">
        <div v-if="orderDetail.status===0">
          <el-row :gutter="20">
            <el-col :span="6" :offset="0">
              <el-form-item label="退款金额" size="mini">
                <el-input-number
                  v-model="form.returnAmount"
                  size="mini"
                  :controls="true"
                  controls-position="both"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6" :offset="0">
              <el-form-item label="处理人" prop="handleMan">
                <el-input v-model="form.handleMan" />
              </el-form-item>
            </el-col>
            <el-col :span="6" :offset="0">
              <el-form-item label="处理备注" prop="handleNote">
                <el-input v-model="form.handleNote" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top:50px;" type="flex" justify="center" :gutter="20">
            <el-col :span="3" :offset="0">
              <el-button style="width:100%;" type="primary" size="small" @click="operateApply(1)">同意退款</el-button>
            </el-col>
            <el-col :span="3" :offset="0">
              <el-button style="width:100%;" size="small" @click="operateApply(0)">拒绝退款</el-button>
            </el-col>
          </el-row>
        </div>

        <div v-if="orderDetail.status!==0">
          <div style="margin-left:10px;" class="to-bold margin-top-30">处理结果</div>
          <el-row style="margin-top:20px;transform:translateX(10px);" :gutter="20">
            <el-col :span="6" :offset="0">
              处理人:{{ orderDetail.handleMan }}
            </el-col>
            <el-col :span="6" :offset="0">
              处理备注:{{ orderDetail.handleNote }}
            </el-col>
            <el-col :span="6" :offset="0">
              处理时间:{{ orderDetail.handleTime }}
            </el-col>
          </el-row>
        </div>

        <div v-if="orderDetail.status===1">
          <el-row style="margin-top:20px;" :gutter="20">
            <el-col :span="6" :offset="0">
              <el-form-item label-width="70px" prop="receiveMan" label="收货人" size="mini">
                <el-input v-model="form.receiveMan" placeholder="收货人" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="6" :offset="0">
              <el-form-item label="收货备注" size="mini" prop="receiveNote">
                <el-input v-model="form.receiveNote" placeholder="收货备注" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top:60px;" type="flex" justify="center" :gutter="20">
            <el-col :span="4" :offset="0">
              <el-button style="width:100%;" type="primary" size="small" @click="operateApply(2)">确认收货</el-button>
            </el-col>
          </el-row>

        </div>

        <div v-if="orderDetail.status===2">
          <div style="margin-left:10px;" class="to-bold margin-top-30">收货信息</div>
          <el-row style="margin-top:20px;margin-left:2px;" :gutter="20">
            <el-col :span="6" :offset="0">
              退单收货人:{{ orderDetail.receiveMan }}
            </el-col>
            <el-col :span="6" :offset="0">
              退单收货备注:{{ orderDetail.receiveNote }}
            </el-col>
            <el-col :span="6" :offset="0">
              退单收货时间:{{ orderDetail.receiveTime }}
            </el-col>
          </el-row>

        </div>
      </el-form>

      <div style="margin-top:100px;" />
    </el-card>

  </div>
</template>

<script>
import mix from '@/mixin'
import {
  orderReturn as orderReturnApi,
  agreeApply as agreeApplyApi,
  rejectApply as rejectApplyApi,
  receiveProduct as receiveProductApi
} from '@/api/order/list'
export default {
  name: 'BackOrderDetail',
  mixins: [mix],
  data() {
    return {
      orderSn: '', // 订单号
      orderDetail: {},
      table: [],
      srcList: [],
      form: {
        handleMan: '',
        returnAmount: 0,
        handleNote: '',
        receiveMan: '',
        receiveNote: ''
      },
      rules: {
        handleMan: [{ required: true, trigger: 'blur', message: '请填写处理人信息' }],
        handleNote: [{ required: true, trigger: 'blur', message: '请填写处理备注' }],
        receiveMan: [{ required: true, trigger: 'blur', message: '请填写收货人姓名' }],
        receiveNote: [{ required: true, trigger: 'blur', message: '请填写收货人备注' }]
      }
    }
  },
  created() {
    this.orderSn = this.$route.query.orderSn
    this.getReturnDetail()
  },
  methods: {
    // 查看订单详情
    goToListDetail() {
      this.$router.push({ name: 'OrderDetail', query: { orderSn: this.orderDetail.orderId }})
    },
    // 验证表单是否正确
    verifyForm(arr) {
      var errorArr = []
      this.$refs.form.validateField(arr, error => {
        errorArr.push(error)
      })
      console.log('error==>')
      for (var i = 0; i < errorArr.length; i++) {
        console.log('err==>', errorArr[i])
        if (errorArr[i]) {
          this.$message.error(errorArr[i])
          return false
        }
      }
      return true
    },
    // 是否同意退款
    operateApply(sn) {
      var flag = true
      this.form.id = this.orderSn
      var api = null
      var arr = []
      if (sn === 1) { // 同意退货
        api = agreeApplyApi
        arr = ['handleMan', 'handleNote']
        flag = this.verifyForm(arr)
      } else if (sn === 0) { // 拒绝退货
        api = rejectApplyApi
      } else if (sn === 2) { // 收到货
        api = receiveProductApi
        arr = ['receiveMan', 'receiveNote']
        flag = this.verifyForm(arr)
      }

      if (flag) {
        api(this.orderSn, this.form)
          .then(res => {
            const { success, message } = res
            if (success) {
              this.$message.success('处理成功!')
              this.getReturnDetail()
            } else {
              this.$message.error(message)
            }
          })
      }
    },
    // 获取退单详情
    getReturnDetail() {
      orderReturnApi(this.orderSn)
        .then(res => {
          const { success, message, data } = res
          if (success) {
            this.orderDetail = data.orderReturnApply
            this.orderDetail.proofPics = this.orderDetail.proofPics ? this.orderDetail.proofPics.split(',') : []
            this.srcList = this.orderDetail.proofPics
            var obj = {
              productPic: this.orderDetail.productPic,
              productName: this.orderDetail.productName,
              productBrand: this.orderDetail.productBrand,
              productRealPrice: this.orderDetail.productRealPrice,
              productAttr: this.orderDetail.productAttr,
              productCount: this.orderDetail.productCount,
              returnAmount: this.orderDetail.returnAmount
            }
            var str = ''
            JSON.parse(obj.productAttr).forEach(ele => {
              str += ele.key + ':' + ele.value + ' '
            })
            obj.productAttr = str
            this.table = []
            this.table.push(obj)
          } else {
            this.$message.error(message)
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.to-bold{
  font-weight: 700;
}
::v-deep .el-form-item__error{
  left:-70px;
}
</style>
